package com.huitu.whht.ribbon_customer;

import com.alibaba.fastjson.JSONObject;
import com.huitu.whht.ribbon_customer.util.RestTemplateUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class HelloService {

    @Autowired
    RestTemplateUtil restTemplateUtil;

    // 熔断错误回调方法
    public String helloFallBack(){
        return "Error occurred!";
    }

    /**
     * 调用Eureka系统中名都为test-service的ribbon_service_a或ribbon_service_b的方法/hello
     * @return
     */
    // 注解指定发生错误时的回调方法
    @HystrixCommand(fallbackMethod="helloFallBack")
    public Object helloService(){
        // Get请求调用服务，restTemplate被@LoadBalanced注解标记，Get方法会自动进行负载均衡
        // restTemplate会交替调用service_a和service_b
        Map<String, Object> params = new HashMap<>();
        params.put("rbCd","FF000000000L0071");
        String url = "http://base-szy-api/hbszy/wr/ab/b/list";
        JSONObject result = restTemplateUtil.postBody(url,params);

        return result;
    }



}
